package com.itbaiyun.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaiyun.usercenter.common.BaseResponse;
import com.itbaiyun.usercenter.common.ErrorCode;
import com.itbaiyun.usercenter.common.ResultUtil;
import com.itbaiyun.usercenter.exception.BusinessException;
import com.itbaiyun.usercenter.model.domain.Team;
import com.itbaiyun.usercenter.model.domain.User;
import com.itbaiyun.usercenter.model.dto.*;
import com.itbaiyun.usercenter.model.vo.TeamUserVO;
import com.itbaiyun.usercenter.service.TeamService;
import com.itbaiyun.usercenter.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 用户组队接口
 */
@CrossOrigin
@RestController
@RequestMapping("/team")
public class TeamController {

    @Resource
    private UserService userService;

    @Resource
    private TeamService teamService;

    /**
     * 创建新的队伍
     *
     * @param teamAddDTO
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addTeam(TeamAddDTO teamAddDTO, HttpServletRequest request) {
        if (teamAddDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR, "队伍信息为空");
        }
        User loginUser = userService.getLoginUser(request);
        Team team = new Team();
        BeanUtils.copyProperties(teamAddDTO, team);
        long TeamID = teamService.addTeam(team, loginUser);
        return ResultUtil.sucess(TeamID);
    }

    /**
     * 删除队伍信息
     *
     * @param id 要删除的队伍id
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeam(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍id不能小于0");
        }
        boolean result = teamService.removeById(id);
        if (!result) {
            throw new RuntimeException("删除失败");
        }
        return ResultUtil.sucess(true);
    }

    /**
     * 更新队伍信息
     *
     * @param teamUpdateDTO
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(TeamUpdateDTO teamUpdateDTO, HttpServletRequest request) {
        if (teamUpdateDTO == null) {
            throw new RuntimeException("队伍信息为空");
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = teamService.updateTeam(teamUpdateDTO, loginUser);
        if (!result) {
            throw new RuntimeException("队伍更新失败");
        }
        return ResultUtil.sucess(true);
    }


    @PostMapping("/get")
    public BaseResponse<Team> getTeamById(long id) {
        if (id <= 0) {
            throw new RuntimeException("队伍id不能小于0");
        }
        Team team = teamService.getById(id);
        if (team == null) {
            throw new RuntimeException("队伍不存在");
        }
        return ResultUtil.sucess(team);
    }

    /**
     * 查询指定队伍信息
     *
     * @param teamQueryDTO
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<TeamUserVO>> listTeam(TeamQueryDTO teamQueryDTO, HttpServletRequest request) {
        if (teamQueryDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR);
        }
        boolean isAdmin = userService.isAdmin(request);
        List<TeamUserVO> result = teamService.listTeams(teamQueryDTO, isAdmin);
        return ResultUtil.sucess(result);
    }


    /**
     * 分页查询队伍
     *
     * @param teamQueryDTO
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<Team>> listTeamByPage(TeamQueryDTO teamQueryDTO) {
        if (ObjectUtils.isEmpty(teamQueryDTO)) {
            throw new RuntimeException("队伍信息为空");
        }
        Team team = new Team();
        BeanUtils.copyProperties(teamQueryDTO, team);
        Page<Team> page = new Page<>(teamQueryDTO.getPageNum(), teamQueryDTO.getPageSize());
        QueryWrapper<Team> queryWrapper = new QueryWrapper(team);
//        Long id = teamQueryDTO.getId();
//        String name = teamQueryDTO.getName();
//        String description = teamQueryDTO.getDescription();
//        String searchText = teamQueryDTO.getSearchText();
//        Integer maxNum = teamQueryDTO.getMaxNum();
//        Long userId = teamQueryDTO.getUserId();
//        Integer status = teamQueryDTO.getStatus();
//        if (StringUtils.isNotBlank(searchText)) {
//            queryWrapper.and(qw -> qw.like("name", searchText).or().like("description", searchText));
//        }
//        if (StringUtils.isNotBlank(description)) {
//            queryWrapper.like("description",description);
//        }
//        if (StringUtils.isNotBlank(name)){
//            queryWrapper.like("name",name);
//        }
//        if (id!=null){
//            queryWrapper.eq("id",id);
//        }
//        if (userId!=null){
//            queryWrapper.eq("userId",userId);
//        }
//        if (teamQueryDTO.getStatus()!=null){
//            queryWrapper.eq("status",teamQueryDTO.getStatus());
//        }
//        if (maxNum!=null){
//            queryWrapper.eq("maxNum",maxNum);
//        }
//        if (status!=null){
//            queryWrapper.eq("status",status);
//        }
        Page<Team> resultPage = teamService.page(page, queryWrapper);
        return ResultUtil.sucess(resultPage);
    }

    /**
     * 加入队伍接口
     *
     * @param teamJoinDTO 要加入的队伍
     * @param request     request
     * @return
     */
    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinDTO teamJoinDTO, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(teamJoinDTO)) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = teamService.joinTeam(teamJoinDTO, loginUser);
        return ResultUtil.sucess(result);
    }

    /**
     * 退出队伍接口
     *
     * @param teamQuiteDTO 要退出的队伍
     * @return
     */
    @PostMapping("/quit")
    public BaseResponse<Boolean> quitTeam(@RequestBody TeamQuitDTO teamQuiteDTO, HttpServletRequest request) {
        if (teamQuiteDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = teamService.quitTeam(teamQuiteDTO, loginUser);
        return ResultUtil.sucess(result);
    }

    /**
     * 队长解散队伍接口
     *
     * @param teamQuiteDTO 要解散的队伍
     * @param request      登录用户
     * @return
     */
    @PostMapping("/deleteTeamByCaptain")
    public BaseResponse<Boolean> deleteTeamByCaptain(@RequestBody TeamQuitDTO teamQuiteDTO, HttpServletRequest request) {
        if (teamQuiteDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = teamService.deleteTeamByCaptain(teamQuiteDTO, loginUser);
        return ResultUtil.sucess(result);
    }
}

